﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#ifndef __MYSQL_WRAPPER_H__
#define __MYSQL_WRAPPER_H__

#pragma once

#ifdef USE_MYSQL_API

#include "filelog.h"
#include <mysql.h>
#include <errmsg.h>

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// CMysqlWrapper
class CMysqlWrapper : public MObject
{
public:
    enum MYSQL_TRY
    {
        MYSQL_TRY_MAX = 5, // 尝试重连5次失败断开连接
    };
public:
    static bool LibraryInit(void);
    static void LibraryExit(void);
    static bool ThreadInit(void);
    static void ThreadExit(void);
public:
    CMysqlWrapper(bool bThreads = false);
    ~CMysqlWrapper(void);
public:
    bool Connect(CString& strHost, CString& strUser, CString& strPassWD, CString& strDB, UInt uPort = MYSQL_PORT, ULong ulFlag = 0);
    bool Connect(PCXStr pszHost, PCXStr pszUser, PCXStr pszPassWD, PCXStr pszDB, UInt uPort = MYSQL_PORT, ULong ulFlag = 0);
    void Disconnect(void);

    void SetFileLog(CFileLog& Log);

    Int  GetStatus(void);
    void SetStatus(Int nStatus = 0);

    bool IsConnect(void);
    bool IsThreads(void);
    bool IsResult(void);

    bool Select(const CString& strQuery);
    bool Select(PCXStr pszQuery);

    bool Check(const CString& strQuery);
    bool Check(PCXStr pszQuery);

    bool Insert(const CString& strQuery);
    bool Insert(PCXStr pszQuery);

    bool Update(const CString& strQuery);
    bool Update(PCXStr pszQuery);

    bool Delete(const CString& strQuery);
    bool Delete(PCXStr pszQuery);

    bool NextResult(void);
    void FreeResult(void);

    ULLong GetRowCount(void);
    MYSQL_ROW GetRow(void);
    // follow GetRow to get row each colum data length
    PULong GetRowEachColLength(void);

    // mysql document : null-mysql-store-result
    // use GetFieldCount & GetAffectedRowCount to check result
    UInt   GetFieldCount(void);
    ULLong GetAffectedRowCount(void);

    // colum field
    UInt   GetColCount(void);
    MYSQL_FIELD* GetField(void);
    MYSQL_FIELD* GetField(UInt uIndex);
    MYSQL_FIELD* GetAllField(void);

    Int  Ping(void);
    UInt GetErrorId(void);
private:
    UInt TryPing(PCXStr pszMethod, PCXStr pszQuery);
private:
    MYSQL*       m_pConnect;
    MYSQL_RES*   m_pResult;
    CFileLog*    m_pLog;
    Int          m_nStatus;
    Int          m_nTryCount;
    UInt         m_uErrorId;
    bool         m_bThreads;
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "MysqlWrapper.inl"

#endif // USE_MYSQL_API

#endif // __MYSQL_WRAPPER_H__
